﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  TestCommon 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/10/16 21:39:30
 * Update Man   :  Joey Liang
 * Update Time  :  2013/10/16 21:39:30
************************************************************************/

using System;
using System.Collections;

namespace IdleTest
{
    public class TestCommon
    {
        /// <summary>
        /// 测试程序的物理目录
        /// </summary>
        public static string TestFilePath = AppDomain.CurrentDomain.BaseDirectory + "\\";

        public static string[] GetEmptyStrings(bool getNull = true)
        {
            if (getNull)
            {
                return new string[] { null, string.Empty, "" };
            }
            else
            {
                return new string[] { string.Empty, "" };
            }
        }

        /// <summary>
        /// 将项添加到数组中
        /// </summary>
        /// <typeparam name="T">数组类型</typeparam>
        /// <param name="array1">原数组</param>
        /// <param name="array2">追加的数组</param>
        public static T[] ArrayPlus<T>(T[] array1, params T[] array2)
        {
            T[] result = null;
            if (array1 == null)
                return result;

            if (array2 == null || array2.Length == 0)
            {
                return array1;
            }

            array2.CopyTo(array1, 0);
            return array1;
        }

        #region Equal
        /// <summary>
        /// 判断两个数组项相等（顺序必须一致）,对数组项使用"Equal方法"校验，
        /// 如果非CTS类型（即自定义），则应在使用本方法前对Equal方法进行重载
        /// </summary>
        public static bool ArrayEqual(Array array1, Array array2)
        {
            bool isCountEqual = CollectionCountEqual(array1, array2);
            if (!isCountEqual || array1 == null || array2 == null)
            {
                return isCountEqual;
            }

            for (int i = 0; i < array1.Length; i++)
            {
                if (!object.Equals(array1.GetValue(i), array2.GetValue(i)))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 判断两个集合项相等（顺序必须一致）,对集合项使用"Equal方法"校验，
        /// 如果非CTS类型（即自定义），则应在使用本方法前对Equal方法进行重载
        /// </summary>
        public static bool ListEqual(IList list1, IList list2)
        {
            bool isCountEqual = CollectionCountEqual(list1, list1);
            if (!isCountEqual || list1 == null || list2 == null)
            {
                return isCountEqual;
            }

            for (int i = 0; i < list1.Count; i++)
            {
                if (!object.Equals(list1[i], list2[i]))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 判断两个集合项相等（顺序必须一致）,对集合项使用"Equal方法"校验，
        /// 如果非CTS类型（即自定义），则应在使用本方法前对Equal方法进行重载
        /// </summary>
        public static bool CollectionEqual(object collection1, object collection2)
        {
            if (collection1 == null && collection2 == null)
            {
                return true;
            }

            if (collection1 is Array && collection2 is Array)
            {
                return ArrayEqual(collection1 as Array, collection2 as Array);
            }

            if (collection1 is IList && collection2 is IList)
            {
                return ListEqual(collection1 as IList, collection2 as IList);
            }

            return object.Equals(collection1, collection1);
        }

        /// <summary>
        /// 验证两个集合的长度是否一致
        /// </summary>
        /// <param name="collection1">要判断的集合1</param>
        /// <param name="collection2">要判断的集合2</param>
        /// <returns>长度相等（两个集合为null或者长度为0以及一个为null另一个长度为0均认为相等）
        /// 返回true，否则返回false</returns>
        public static bool CollectionCountEqual(ICollection collection1, ICollection collection2)
        {
            if ((collection1 == null || collection1.Count < 1)
                && (collection2 == null || collection2.Count < 1))
            {
                return true;
            }
            else if ((collection1 == null || collection1.Count < 1)
                || (collection2 == null || collection2.Count < 1))
            {
                return false;
            }

            return collection1.Count == collection2.Count;
        }

        #endregion

        #region Foreach Handle
        /// <summary>
        /// 对数组执行简单遍历
        /// </summary>
        /// <typeparam name="T">数组类型</typeparam>
        /// <param name="array">需要遍历的数组</param>
        /// <param name="action">遍历每一项所需执行的委托</param>
        public static void ForeachHandle<T>(T[] array, Action<T> action)
        {
            if (array == null)
                return;

            foreach (T item in array)
            {
                action(item);
            }
        }

        /// <summary>
        /// 对多维数组执行简单遍历,仅支持多维可变数组，如int[]、int[][]、int[][][],不支持形如int[,]标准数组
        /// </summary>
        /// <typeparam name="T">最终执行的类型参数，如string[][][]则为string</typeparam>
        /// <param name="array">需要遍历的数组</param>
        /// <param name="action">遍历每一项所需执行的委托</param>
        /// <param name="loopCount">循环嵌套数</param>
        public static void ForeachHandle<T>(Array array, Action<T> action, int loopCount = 2)
        {
            if (array == null)
            {
                return;
            }

            if (loopCount < 2)
            {
                ForeachHandle<T>(array as T[], action);
                return;
            }

            loopCount--;
            foreach (var item in array)
            {
                ForeachHandle<T>(item as Array, action, loopCount);
            }
        }

        /// <summary>
        /// 对两个数组执行简单交叉遍历
        /// </summary>
        /// <typeparam name="T1">第一个数组类型</typeparam>
        /// <param name="array1">需要遍历的第一个数组</param>
        /// <typeparam name="T2">第二个数组类型</typeparam>
        /// <param name="array2">需要遍历的第二个数组</param>
        /// <param name="action">交叉遍历每一项所需执行的委托</param>
        public static void ForeachHandle<T1, T2>(T1[] array1, T2[] array2, Action<T1, T2> action)
        {
            if (array1 == null || array2 == null)
                return;

            foreach (T1 item1 in array1)
            {
                foreach (var item2 in array2)
                {
                    action(item1, item2);
                }
            }
        }

        #endregion
    }
}
